package com.yanqu.road.server.manager.union;

import com.yanqu.road.server.manager.unioncampwar.CrossUnionCampWarLockGroupMgr;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroup;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroupUnion;
import com.yanqu.road.logic.rank.UnionRankMsgModel;
import com.yanqu.road.server.manager.union.activitygroup.Cross3UnionActivityGroupMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossUnionActivityAbilityRankMgr {
    //activityId 巅峰活动报名期缓存
    private static Map<Integer, CrossActivityUnionAbilityRankInfo> rankInfoMap = new ConcurrentHashMap<>();
    //centerGroup groupId*10+camp 商帮报名期榜单缓存
    private static Map<Integer, Map<Integer, CrossUnionAbilityRankInfo>> unionCampWarGroupRankMap = new ConcurrentHashMap<>();
    private static Map<Integer, CrossActivityUnionAbilityRankInfoGroup> rankInfoGroupMap = new ConcurrentHashMap<>();

    public static CrossActivityUnionAbilityRankInfo getRankInfo(int activityId, List<Long> serverIds, int rankNum) {
        CrossActivityUnionAbilityRankInfo rankInfo = rankInfoMap.get(activityId);
        if (null == rankInfo) {
            synchronized (rankInfoMap) {
                rankInfo = rankInfoMap.get(activityId);
                if (null == rankInfo) {
                    rankInfo = new CrossActivityUnionAbilityRankInfo(activityId);
                    rankInfoMap.put(activityId, rankInfo);
                }
            }
        }

        synchronized (rankInfo) {
            if (rankInfo.isNeedUpdate()) {
                rankInfo.setRankMsg(CrossUnionMgr.getTopMaxAbilityUnionListMsg(activityId, serverIds, rankNum));
            }
        }

        return rankInfo;
    }

    /**
     * 有分组的用这个方法取
     *
     * @param activityId
     * @param groupId
     * @param rankNum
     * @return
     */
    public static CrossActivityUnionAbilityRankInfo getRankInfoByGroup(int activityId, int groupId, int rankNum) {
        UnionActivityGroup groupData = Cross3UnionActivityGroupMgr.getGroupData(activityId, groupId);
        if (groupData == null) {
            return null;
        }
        CrossActivityUnionAbilityRankInfoGroup infoGroup = rankInfoGroupMap.get(activityId);
        if (null == infoGroup) {
            synchronized (rankInfoGroupMap) {
                infoGroup = rankInfoGroupMap.get(activityId);
                if (null == infoGroup) {
                    infoGroup = new CrossActivityUnionAbilityRankInfoGroup(activityId);
                    rankInfoGroupMap.put(activityId, infoGroup);
                }
            }
        }
        CrossActivityUnionAbilityRankInfo rankInfo = infoGroup.getRankInfo(groupId);
        synchronized (rankInfo) {
            if (rankInfo.isNeedUpdate()) {
                List<String> unionIds = new ArrayList<>(groupData.getUnionDataMap().keySet());
                rankInfo.setRankMsg(CrossUnionMgr.getTopMaxAbilityUnionListMsgByUnionIds(activityId, unionIds, rankNum, groupId));
            }
        }

        return rankInfo;
    }

    public static void checkRankMsgOver() {
        List<Integer> removeActivityId = new ArrayList<>();
        long now = DateHelper.getCurrentSecond();
        synchronized (rankInfoMap) {
            for (CrossActivityUnionAbilityRankInfo model : rankInfoMap.values()) {
                if (model.getActivityEndTime() > 0 && now >= model.getActivityEndTime()) {
                    removeActivityId.add(model.getActivityId());
                }
            }
        }

        if (!removeActivityId.isEmpty()) {
            synchronized (rankInfoMap) {
                for (Integer activityId : removeActivityId) {
                    rankInfoMap.remove(activityId);
                }
            }
        }

        removeActivityId.clear();
        synchronized (unionCampWarGroupRankMap) {
            for (Map<Integer, CrossUnionAbilityRankInfo> infoMap : unionCampWarGroupRankMap.values()) {
                for (CrossUnionAbilityRankInfo rankInfo : infoMap.values()) {
                    if (rankInfo.getEndTime() > 0 && now >= rankInfo.getEndTime()) {
                        removeActivityId.add(rankInfo.getCenterGroupId());
                        break;
                    }
                }
            }
        }
        if (!removeActivityId.isEmpty()) {
            synchronized (unionCampWarGroupRankMap) {
                for (Integer centerGroupId : removeActivityId) {
                    unionCampWarGroupRankMap.remove(centerGroupId);
                }
            }
            removeActivityId.clear();
        }

    }

    public static CrossUnionAbilityRankInfo getUnionCampWarGroupRankInfo(int centerGroupId, int groupId, int campId) {
        int keyId = groupId * 10 + campId;
        Map<Integer, CrossUnionAbilityRankInfo> centerDataMap = unionCampWarGroupRankMap.get(centerGroupId);
        if (null == centerDataMap) {
            synchronized (unionCampWarGroupRankMap) {
                centerDataMap = unionCampWarGroupRankMap.get(centerGroupId);
                if (null == centerDataMap) {
                    centerDataMap = new ConcurrentHashMap<>();
                    unionCampWarGroupRankMap.put(centerGroupId, centerDataMap);
                }
            }
        }
        CrossUnionAbilityRankInfo rankInfo = centerDataMap.get(keyId);
        if (null == rankInfo) {
            synchronized (unionCampWarGroupRankMap) {
                rankInfo = centerDataMap.get(keyId);
                if (null == rankInfo) {
                    rankInfo = new CrossUnionAbilityRankInfo(keyId, centerGroupId);
                    centerDataMap.put(keyId, rankInfo);
                }
            }
        }

        synchronized (rankInfo) {
            if (rankInfo.isNeedUpdate()) {
                if (!rankInfo.getRankMsg().isEmpty()){
                    //商帮报名期截止前5分钟不再变更榜单
                    if (ConfigMgr.getUnionCampWarConfig().isRegistrationEndTime()){
                        return rankInfo;
                    }
                }
                rankInfo.setRankMsg(CrossUnionCampWarLockGroupMgr.getCampUnionRankMsgList(centerGroupId, groupId, campId));
                rankInfo.setEndTime(ConfigMgr.getUnionCampWarConfig().getNowWeekBattleTime() + DateHelper.MINUTE_SECONDS * 5);
            }
        }

        return rankInfo;
    }
}
